<template>
	<div class="admin-job-edit">
		<x-border :title="viewModel.name" type="accent" v-if="async">
			<el-alert v-if="viewModel" style="margin-bottom:5px" :title="viewModel.name" type="success" :description="viewModel.intro" :closable="false" show-icon></el-alert>
			<el-form style="padding: 0 20px;">
				<el-form-item label="是否启用" required>
					<el-switch v-model="viewModel.isEnable"></el-switch>
					<div class="demo_intro">关闭后，该策略任务不执行。开启策略，比较消耗服务器的资源，建议根据实际业务按需开启。</div>
				</el-form-item>
				<el-form-item :label="viewModel.intervalOutput.label" required>
					<el-select v-model="viewModel.interval" :placeholder="'请选择'+viewModel.intervalOutput.label">
						<el-option v-for="item in viewModel.intervalOutput.keyValues" :key="item.key" :label="item.value" :value="item.key">
						</el-option>
					</el-select>
					<div class="demo_intro">间隔执行周期，已最后一次执行时间为基准,没两次的执行时间有一定的偏差，大概偏差在30s到-1分钟之间，在设置周期的时候，请注意.</div>
				</el-form-item>
			</el-form>
			<zk-auto-form :serviceConfig="viewModel.autoForm" :showBotton="false" ref="zkAutoForm" style="padding: 0 20px;"></zk-auto-form>
			<el-form style="padding: 0 20px;">
				<el-form-item label=" " style="border-top: 1px solid #ebedf2;padding: 20px 0;">
					<el-button type="primary" @click="taskSave" :loading="loading">保存</el-button>
				</el-form-item>
			</el-form>
		</x-border>
		<div v-else v-loading="isLoading"></div>
		<el-dialog title="保存策略" :visible.sync="dialogVisible" v-if="viewModel" width="40%">
			<div class="admin-reward_dialog">
				<div class="modal-body">
					<el-alert title="修改策略可能会对系统数据造成一定的影响。请慎重！" type="warning" :closable="false">
					</el-alert>
					<el-form class="reward_dialog-form" @submit.native.prevent>
						<el-form-item label="支付密码" :required="true">
							<el-input type="password" v-model="viewModel.payPassword" @keyup.enter.native="save"></el-input>
						</el-form-item>
					</el-form>
				</div>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false" class="footer_cancel">取 消</el-button>
				<el-button type="primary" @click="save" :loading="loading">确定</el-button>
			</span>
		</el-dialog>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				viewModel: '',
				async: false,
				loading: false,
				dialogVisible: false,
				isLoading: true
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			async init() {
				var para = {}
				if (this.$route.query.id) {
					para.id = this.$route.query.id
					this.viewModel = await this.$ala.httpGet('api/taskplan/GetEditView', para)
					this.async = true
				} else {
					this.$ala.error('请传入任务Id')
				}
				this.isLoading = false
			},
			async save() {
				this.loading = true
				this.viewModel.value = JSON.stringify(this.$refs.zkAutoForm.viewModel)
				
				var response = await this.$ala.httpPost('api/taskplan/Save', this.viewModel)
				if (response && response.status === 1) {
					this.$ala.toMessage(response)
				} else {
					this.dialogVisible = false
				}
				this.loading = false
			},
			async taskSave() {
				this.dialogVisible = true
			}
		}
	}
</script>
<style lang="scss">
	@import "./var.scss";
</style>